
import javax.microedition.lcdui.*;


public class Sprite
{
    Image[] img;
    Graphics g;
    byte paleta;

    final short modul_w = 7;
    final short modul_h = 9;
    final short[] mapping = new short[]
    {
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, 26, 27,
        28, 29, 30, 31, 32, 33, 34, 35, 36, -1,
        -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
        5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
        25, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    };

    public Sprite(Graphics _g)
    {
        g = _g;
        img = new Image[2];
        try
        {
            img[0] = Image.createImage("/fuente0.png");
            img[1] = Image.createImage("/fuente1.png");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        paleta = 0;
    }

    public void setCurrentPalette(int p)
    {
        paleta = (byte) p;
    }

    public void drawString(byte[] b, int x, int y, int anchor)
    {
        int img_y = 1;
        int mod_s = 1;

        //System.out.print("T->: ");

        if((anchor & Graphics.HCENTER) != 0)
        {
            x -= (b.length>>1) * (modul_w - 1);
        }
        else if((anchor & Graphics.RIGHT) != 0)
        {
            x -= b.length * (modul_w - 1);
        }
        if((anchor & Graphics.VCENTER) != 0)
        {
            y -= (modul_h>>1);
        }

        for(int i=0; i < b.length; i++)
        {
            //System.out.print(b[i]);
            if(b[i] == 32)
            {
                continue;
            }
            if(b[i] > mapping.length || b[i] < 0)
            {
                System.out.print("T-> : Fuera de rango: " + b[i]);
                continue;
            }
            if(mapping[b[i]] == -1)
            {
                System.out.print("T-> : Caracter no mapeado: " + b[i]);
                continue;
            }
            try
            {
                g.drawRegion(img[paleta], mapping[b[i]] * modul_w, img_y, modul_w, modul_h, 0, x + i * (mod_s + modul_w), y, anchor);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        //System.out.println();
    }

    public void drawString(String s, int x, int y, int anchor)
    {
        drawString(s.getBytes(), x, y, anchor);
    }
}
